package subject1.laserKnife;

/**
 * @Author CandyWall
 * @Date 2022/8/19--2:05
 * @Description 激光刀
 */
public class LaserKnife {
    public int getUnitBlockNum(int height, int width, String operations, int[] distances) {
        boolean[][] verticalCut = new boolean[height + 1][width + 1];
        boolean[][] horizontalCut = new boolean[height + 1][width + 1];
        boolean open = false;
        int x = 0;
        int y = 0;
        int deltaX = 0;
        int deltaY = 1;
        int j = 0;
        for (int i = 0; i < operations.length(); i++) {
            char operation = operations.charAt(i);
            switch (operation) {
                case 'M':
                    if (open) {
                        for (int k = 0; k < distances[j]; k++) {
                            if (deltaX == 1) {
                                horizontalCut[y][x + k * deltaX] = true;
                            } else if(deltaX == -1) {
                                horizontalCut[y][x + k * deltaX - 1] = true;
                            }
                            if (deltaY == 1) {
                                verticalCut[y + k * deltaY][x] = true;
                            } else if(deltaY == -1) {
                                verticalCut[y + k * deltaY - 1][x] = true;
                            }
                        }
                    }
                    x += distances[j] * deltaX;
                    y += distances[j] * deltaY;
                    j++;
                    break;
                case 'O':
                    open = true;
                    break;
                case 'C':
                    open = false;
                    break;
                case 'U':
                    deltaX = 0;
                    deltaY = -1;
                    break;
                case 'D':
                    deltaX = 0;
                    deltaY = 1;
                    break;
                case 'L':
                    deltaX = -1;
                    deltaY = 0;
                    break;
                case 'R':
                    deltaX = 1;
                    deltaY = 0;
                    break;
            }
        }

        int squareCount = 0;
        // 边缘默认都是被切状态
        // 横向
        for (int i = 0; i < width + 1; i++) {
            horizontalCut[0][i] = true;
            horizontalCut[height][i] = true;
        }
        for (int k = 0; k < height + 1; k++) {
            verticalCut[k][0] = true;
            verticalCut[k][width] = true;
        }
        // 纵向
        
        for (int i = 0; i < height - 1; i++) {
            for (int k = 0; k < width - 1; k++) {
                if (horizontalCut[i][k] && horizontalCut[i + 1][k] && verticalCut[i][k] && verticalCut[i][k + 1]) {
                    squareCount++;
                }
            }
        }
        return squareCount;
    }

    public static void main(String[] args) {
        System.out.println(new LaserKnife().getUnitBlockNum(3, 4, "MRMOMDMLMUMCRMODMC", new int[]{1, 1, 2, 1, 2, 2, 1, 2}));

        System.out.println(new LaserKnife().getUnitBlockNum(3, 4, "MRMOMDMLMUMC", new int[]{1, 1, 2, 1, 2, 2}));

        System.out.println(new LaserKnife().getUnitBlockNum(3, 3, "MROMDMLMUMRMDMRMUMLMC", new int[]{1, 2, 1, 2, 1, 1, 1, 1, 1, 1}));
    }
}
